package weaver.jobs.zhlx.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.zhlx.duss.utils.ZhlxJobUtil;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-09-10
 * @Description: 定时调用SAP-FI023接口同步内部订单号到建模台账
 * @ModifyLog:
 **/

public class WeaSynXsddtzBySapCron extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            ZhlxJobUtil jobUtil = new ZhlxJobUtil();
            String tablename = "uf_zhlxxsddtz";
            int formmodeid = jobUtil.getFormmodeid(tablename);
            JSONObject dataJson = new JSONObject();
            dataJson.put("VBELN", "");
            JSONObject resultJson = jobUtil.handleOKHttpRequest("FI006", dataJson);
            String code = Util.null2String(resultJson.getString("code"));
            baseBean.writeLog(">>>>>WeaSynXsddtzBySapCron>>>>>code=" + code);
            if ("success".equals(code)) {
                JSONObject outputJson = resultJson.getJSONObject("OUTPUT");
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                baseBean.writeLog(">>>>>WeaSynXsddtzBySapCron>>>>>msgType=" + msgType + ",msgText=" + msgText);
                if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                    JSONObject dataReturnJson = outputJson.getJSONObject("DATA");
                    JSONArray returnList = dataReturnJson.getJSONArray("DATAITEM");
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject paramJson = new JSONObject();
                        JSONObject returnJson = returnList.getJSONObject(i);
                        String xsddbh = returnJson.getString("VBELN");
                        paramJson.put("xsddbh", xsddbh);
                        paramJson.put("xsddlxbh", returnJson.getString("AUART"));
                        paramJson.put("cjrq", returnJson.getString("ERDAT"));
                        paramJson.put("cjr", returnJson.getString("ERNAM"));
                        paramJson.put("yqjhrq", returnJson.getString("VDATU"));
                        paramJson.put("gsdm", returnJson.getString("BUKRS_VF"));
                        paramJson.put("gsmc", returnJson.getString("BUTXT"));
                        paramJson.put("xszzbh", returnJson.getString("VKORG"));
                        paramJson.put("xszzms", returnJson.getString("VTEXT"));
                        paramJson.put("fxqdbh", returnJson.getString("VTWEG"));
                        paramJson.put("fxqdms", returnJson.getString("VTEXT_FX"));
                        paramJson.put("cpzbh", returnJson.getString("SPART"));
                        paramJson.put("cpzms", returnJson.getString("VTEXT_CPZ"));
                        paramJson.put("xsbscbh", returnJson.getString("VKBUR"));
                        paramJson.put("xsbscms", returnJson.getString("BEZEI_BSC"));
                        paramJson.put("khbh", returnJson.getString("KUNNR"));
                        paramJson.put("khmc", returnJson.getString("NAME1"));
                        paramJson.put("zftjbm", returnJson.getString("ZTERM"));
                        paramJson.put("zftjms", returnJson.getString("VTEXT_ZFTJ"));
                        paramJson.put("hsje", returnJson.getString("KZWI1"));
                        paramJson.put("hb", returnJson.getString("WAERK"));
                        int mainid = jobUtil.getMainId(tablename, "xsddbh", xsddbh);
                        if (mainid <= 0) {
                            jobUtil.insertModelData(formmodeid, paramJson);
                        } else {
                            jobUtil.updateModelData(tablename, mainid, paramJson);
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>WeaSynXsddtzBySapCron>>>>>Exception=" + e);
        }

    }
}

